package com.small.site.controller;

import com.small.site.aspect.annotation.Log;
import com.small.site.service.PermissionService;
import com.small.site.util.R;
import com.small.site.validation.ValidationType;
import com.small.site.vo.PermissionVo;
import com.small.site.vo.req.PermissionReqVo;
import com.small.site.vo.req.PermissionRespNodeTreeVo;
import com.small.site.vo.resp.PermissionRespNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaochi
 * @date 2021/12/16 13:45
 * @desc PermissionController
 */
@RestController
@RequestMapping("/manager")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    @Log(title = "组织管理-菜单权限管理",action = "获取所有的菜单权限数据接口")
    @PreAuthorize("hasAuthority('sys:role:add') OR hasAuthority('sys:role:update')")
    @GetMapping("/permissions/tree/all/v1")
    public R<List<PermissionRespNodeVo>> getAllPermissionTree(){
        return R.ok(permissionService.selectAllTree());
    }

    @PreAuthorize("hasAuthority('sys:permission:list')")
    @Log(title = "组织管理-菜单权限管理",action = "权限表格列表数据展示")
    @GetMapping("/permissions/v1")
    public R<List<PermissionVo>> getAllPermission(){
        return R.ok(permissionService.selectAll());
    }

    @Log(title = "组织管理-菜单权限管理",action = "新增菜单权限接口")
    @PreAuthorize("hasAuthority('sys:permission:update') OR hasAuthority('sys:permission:add')")
    @GetMapping("/permissions/tree/v1")
    public R<List<PermissionRespNodeTreeVo>> getAllPermissionTreeExBtn(){
        return R.ok(permissionService.selectAllMenuByTree());
    }

    @PreAuthorize("hasAuthority('sys:permission:add')")
    @Log(title = "组织管理-菜单权限管理",action = "新增菜单权限接口")
    @PostMapping("/permission/v1")
    public R<String> createPermission(@RequestBody @Valid PermissionReqVo permissionReqVo){
        return permissionService.addPermission(permissionReqVo.buildEntity());
    }

    @PreAuthorize("hasAuthority('sys:permission:update')")
    @Log(title = "组织管理-菜单权限管理",action = "编辑菜单权限接口")
    @PutMapping("/permission/v1")
    public R<String> updatePermission(@RequestBody @Validated(ValidationType.update.class) PermissionReqVo permissionReqVo){
        return permissionService.updatePermission(permissionReqVo.buildEntity());
    }

    @PreAuthorize("hasAuthority('sys:permission:delete')")
    @Log(title = "组织管理-菜单权限管理",action = "删除菜单权限接口")
    @DeleteMapping("/permission/v1/{permissionId}")
    public R<String> deletedPermission(@PathVariable("permissionId") String permissionId){
        return permissionService.deletedPermission(permissionId);
    }
}
